//
//  NSString+format.m
//  JCTool
//
//  Created by gome on 2018/5/11.
//  Copyright © 2018年 gome. All rights reserved.
//

#import "NSString+Extension.h"
#import "NSObject+Extension.h"

@implementation NSString (Extension)
//字符串是否为空
- (BOOL)jc_isEmptyString {
    
    if ([self jc_isEmptyObject] && self.length > 0) {
        return YES;
    } else {
        return NO;
    }
}


///1234567 格式化后为 1,234,567 symbol 为 @“,”
- (NSString *)jc_formatWithSymbol:(NSString *)symbol {
    if (self == nil || symbol == nil) {
        return nil;
    }
    
    int count = 0;
    long long int num, tmp = self.longLongValue;
    num = tmp;

    while (tmp != 0) {
        count++;
        tmp /= 10;
    }
    
    NSMutableString *originString = [NSMutableString stringWithString:[NSString stringWithFormat:@"%ld", (long)num]];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 3) {
        count -= 3;
        NSRange rang = NSMakeRange(originString.length - 3, 3);
        NSString *str = [originString substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:symbol atIndex:0];
        [originString deleteCharactersInRange:rang];
    }
    
    [newstring insertString:originString atIndex:0];
    return newstring;
}

///手机号码格式化 166-0280-8396 166 0280 8396
- (NSString *)jc_formatPhoneNumWithSymbol:(NSString *)symbol {
    
    if (self == nil || symbol == nil) {
        return nil;
    }
    
    int count = 0;
    long long int num, tmp = self.longLongValue;
    num = tmp;
    
    while (tmp != 0) {
        count++;
        tmp /= 10;
    }
    
    NSMutableString *originString = [NSMutableString stringWithString:[NSString stringWithFormat:@"%ld", (long)num]];
    NSMutableString *newstring = [NSMutableString string];
    while (count > 4) {
        count -= 4;
        NSRange rang = NSMakeRange(originString.length - 4, 4);
        NSString *str = [originString substringWithRange:rang];
        [newstring insertString:str atIndex:0];
        [newstring insertString:symbol atIndex:0];
        [originString deleteCharactersInRange:rang];
    }
    
    [newstring insertString:originString atIndex:0];
    return newstring;
}

///手机号码加密 166****8396
- (NSString *)jc_encryptionPhoneNumWithSymbol:(NSString *)symbol {
    
    if (self == nil || symbol == nil) {
        return nil;
    }
    
    NSMutableString *originString = self.mutableCopy;
    if (originString.length < 11) {
        return nil;
    }
    
    NSRange rang = NSMakeRange(3, 4);
    NSString *replaceString = [NSString stringWithFormat:@"%@%@%@%@", symbol, symbol, symbol, symbol];
    [originString replaceCharactersInRange:rang withString:replaceString];
    
    return originString.copy;
}

//home 文件夹路径
- (NSString *)jc_homePath {
    return NSHomeDirectory().copy;
}

///caches 文件夹路径
- (NSString *)jc_cachePath {
    return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject].copy;
}
///临时文件夹路径
- (NSString *)jc_libraryTempPath {
    return NSTemporaryDirectory().copy;

}
///document文件夹路径
- (NSString *)jc_documentPath {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject].copy;
}

//library路径
- (NSString *)jc_libraryPath {
     return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES) firstObject].copy;
}

@end
